using System;
using ClockResolution = Me.Prettyprint.Hector.Api.ClockResolution;

namespace Me.Prettyprint.Cassandra.Service.Clock
{
	[Serializable]
	public class MicrosecondsSyncClockResolution:AbstractClockResolution, ClockResolution
	{
		private const long ONE_THOUSAND = 1000L;
		/// <summary> The last time value issued. Used to try to prevent duplicates.</summary>
		private static long lastTime = - 1;
		public virtual long CreateClock()
		{
			// The following simulates a microseconds resolution by advancing a static counter
			// every time a client calls the createClock method, simulating a tick.
			long us = SystemMilliseconds * ONE_THOUSAND;
			// Synchronized to guarantee unique time within and across threads.
			lock (typeof(MicrosecondsSyncClockResolution))
			{
				if (us > lastTime)
				{
					lastTime = us;
				}
				else
				{
					// the time i got from the system is equals or less
					// (hope not - clock going backwards)
					// One more "microsecond"
					us = ++lastTime;
				}
			}
			return us;
		}
	}
}